package com.javaforever.clocksimplejee4.facade;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Map;
import java.util.Set;
import java.util.TreeMap;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.javaforever.clocksimplejee4.domain.User;
import com.javaforever.clocksimplejee4.service.PrivilegeService;
import com.javaforever.clocksimplejee4.service.UserService;
import com.javaforever.clocksimplejee4.serviceimpl.PrivilegeServiceImpl;
import com.javaforever.clocksimplejee4.serviceimpl.UserServiceImpl;
import com.javaforever.clocksimplejee4.utils.AppTokenManager;

import net.sf.json.JSONObject;

public class LogoutFacade extends HttpServlet {
	private static final long serialVersionUID = 2148070674786179357L;

	protected void processRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        Map<String, Object> result = new TreeMap<String,Object>();
    	String token = request.getParameter("token");    	
    	AppTokenManager.removeUserIdByToken(token);
    	request.getSession().setAttribute("islogin",null);
    	request.getSession().setAttribute("isadmin",null);
    	request.getSession().setAttribute("userData",null);
    	request.getSession().setAttribute("canDelete",null);
    	request.getSession().setAttribute("forwardUrl",null);
    	result.put("success", true);
    	result.put("forwardUrl", "../login/index.html");
    	out.print(JSONObject.fromObject(result));
    }

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        processRequest(request, response);
        
    }


    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        processRequest(request, response);
    }


    @Override
    public String getServletInfo() {
        return "Short description";
    }

}


